{% extends "base.html" %}

{% block title %}用户管理{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col">
            <h2 class="mb-3">用户管理</h2>
        </div>
    </div>

    <!-- 添加搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                        <input type="text" class="form-control" name="search" value="{{ search }}" 
                               placeholder="搜索用户名、姓名或部门">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="role">
                        <option value="">所有角色</option>
                        <option value="engineer" {% if role == 'engineer' %}selected{% endif %}>测控工程师</option>
                        <option value="manager" {% if role == 'manager' %}selected{% endif %}>项目管理人员</option>
                        <option value="integrator" {% if role == 'integrator' %}selected{% endif %}>系统集成商</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i>搜索
                    </button>
                </div>
                <div class="col-md-2">
                    <a href="{{ url_for('main.users') }}" class="btn btn-secondary w-100">
                        <i class="fas fa-redo me-1"></i>重置
                    </a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>用户名</th>
                            <th>姓名</th>
                            <th>角色</th>
                            <th>部门</th>
                            <th>联系电话</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in pagination.items %}
                        <tr>
                            <td>{{ user.username }}</td>
                            <td>{{ user.full_name }}</td>
                            <td>
                                {% if user.role == 'engineer' %}
                                测控工程师
                                {% elif user.role == 'manager' %}
                                项目管理人员
                                {% else %}
                                系统集成商
                                {% endif %}
                            </td>
                            <td>{{ user.department }}</td>
                            <td>{{ user.phone }}</td>
                            <td>
                                <a href="{{ url_for('main.user_detail', user_id=user.id) }}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-info-circle"></i> 详情
                                </a>
                                <a href="{{ url_for('main.edit_user', user_id=user.id) }}" class="btn btn-sm btn-outline-secondary">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 添加分页控件 -->
            {% if pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{% if pagination.has_prev %}{{ url_for('main.users', page=pagination.prev_num, search=search, role=role) }}{% else %}#{% endif %}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('main.users', page=page, search=search, role=role) }}">{{ page }}</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{% if pagination.has_next %}{{ url_for('main.users', page=pagination.next_num, search=search, role=role) }}{% else %}#{% endif %}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            <div class="text-center text-muted">
                显示第 {{ pagination.total }} 条记录中的第 {{ (pagination.page - 1) * pagination.per_page + 1 }} 到 
                {{ min(pagination.page * pagination.per_page, pagination.total) }} 条
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 